package com.harry.martin_skill_master.view.activities;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.harry.martin_skill_master.R;
import com.harry.martin_skill_master.adapter.PagerFragmentAdapter;
import com.harry.martin_skill_master.base.BaseActivity;
import com.harry.martin_skill_master.util.ActivityCollector;
import com.harry.martin_skill_master.util.NoScrollViewPager;
import com.harry.martin_skill_master.view.fragments.PunstFragment;
import com.harry.martin_skill_master.view.fragments.RecommendFragment;
import com.harry.martin_skill_master.view.fragments.VideoFragment;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Martin-harry
 * @date 2021/8/10
 * @address
 * @Desc 导航栏、白/夜间模式的切换
 */
public class MainActivity extends BaseActivity {

    //设置标题
    @Titles
    private static final String[] mTitles = {"推荐", "视频", "段子"};

    //设置选中图标
    @SeleIcons
    private static final int[] mSelectIcons = {R.drawable.recommend_selector, R.drawable.video_selector, R.drawable.punst_selector};

    //设置未选中图标
    @NorIcons
    private static final int[] mNormalIcon = {R.drawable.recommend_normal, R.drawable.video_normal, R.drawable.punst_normal};

    private NoScrollViewPager mVp;
    private JPTabBar taBar;
    private List<Fragment> list;
    private PagerFragmentAdapter adapter;

    private View view;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private long exitTime = 0;
    private DrawerLayout drawerLayout;
    private ImageView my_photo;
    private Switch mainSwitch;

    @Override
    public View addContentLayout() {
        view = getLayoutInflater().inflate(R.layout.activity_main, baseLayout, false);
        return view;
    }

    @Override
    public void initViews() {
        setTitle("");

        drawerLayout = findView(R.id.main_drawerLayout);
        my_photo = findView(R.id.my_photo);
        mVp = view.findViewById(R.id.vp);
        taBar = view.findViewById(R.id.taBar);
        mainSwitch = findView(R.id.main_switch);
    }

    @Override
    public void initListener() {
        my_photo.setOnClickListener(this);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void initData() {
        //取出保存的值（取数据）
        preferences = getSharedPreferences("appcompat", MODE_PRIVATE);
        editor = preferences.edit();

        //获取状态并设置当前状态
        boolean isChecked = preferences.getBoolean("isChecked", false);
        mainSwitch.setChecked(isChecked);
        /**
         * 白/夜间模式的切换
         */
        mainSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    editor.putBoolean("isChecked", true);
                    editor.commit();

                } else {
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    editor.putBoolean("isChecked", false);
                    editor.commit();
                }
            }
        });

        list = new ArrayList<>();
        try {
            list.add(RecommendFragment.class.newInstance());
            list.add(VideoFragment.class.newInstance());
            list.add(PunstFragment.class.newInstance());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        taBar.setContainer(mVp);

        //设置适配器
        adapter = new PagerFragmentAdapter(getSupportFragmentManager(), list);
        mVp.setAdapter(adapter);
        mVp.setNoScroll(true);//禁止滑动
    }

    @Override
    public void onSingleClick(View v) {
        switch (v.getId()) {
            case R.id.my_photo:
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
        }
    }

    @Override
    public void onMultiClick(View v) {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        drawerLayout.closeDrawer(Gravity.LEFT);
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                ActivityCollector.finishAll();
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
